/*
 * buffer_read.hpp
 *
 *  Created on: 2016年4月8日
 *      Author: win7
 */


	bool read(char* buf, int32_t length){
		return read((unsigned char*)(buf), length);
	}
	bool read(buffer_impl& buffer){
		int32_t len = this->length();
		unsigned char* buf = buffer.get_write_buffer(len);
		if(nullptr == buf){
			return false;
		}
		if(read(buf, len)){
			buffer.commit_write(len);
			return true;
		}
		return false;
	}
	bool read(buffer_impl& buffer, int32_t length){
		unsigned char* buf = buffer.get_write_buffer(length);
		if(nullptr == buf){
			return false;
		}
		if(read(buf, length)){
			buffer.commit_write(length);
			return true;
		}
		return false;
	}
	bool read(unsigned char& value){
		return read(static_cast<unsigned char*>(&value), sizeof(value));
	}
	bool read(unsigned short& value){
		if(read((unsigned char*)(&value), sizeof(value))){
			value = ::ntohs(value);
			return true;
		}
		return false;
	}
	bool read(unsigned int& value){
		switch(sizeof(value)){
		case 2:
			return read((unsigned short &)(value));
		case 4:
			if(read((unsigned char*)(&value), sizeof(value))){
				value = ::ntohl(static_cast<uint32_t>(value));
				return true;
			}
			return false;
		case 8:
			return read((unsigned long long &)(value));
		default:
			return false;
		}
	}
	bool read(unsigned long& value){
		switch(sizeof(value)){
		case 4:
			if(read((unsigned char*)(&value), sizeof(value))){
				value = ::ntohl(static_cast<uint32_t>(value));
				return true;
			}
			return false;
		case 8:
			return read((unsigned long long &)(value));
		default:
			return false;
		}
	}
	bool read(unsigned long long& value){
//		uint32_t low = ::htonl(static_cast<uint32_t>(value&0xFFFFFFFF));
//		uint32_t high = ::htonl(static_cast<uint32_t>((value >> 32)&0xFFFFFFFF));
//		value = (static_cast<uint64_t>(low) << 32) | high;
		if(read((unsigned char*)(&value), sizeof(value))){
			value = (static_cast<uint64_t>(::ntohl(static_cast<uint32_t>(value&0xFFFFFFFF))) << 32) | ::ntohl(static_cast<uint32_t>((value >> 32)&0xFFFFFFFF));
			return true;
		}
		return false;
	}

	bool read(signed char& value){
		return read((unsigned char*)(&value), sizeof(value));
	}
	bool read(signed short& value){
		return read((unsigned short &)(value));
	}
	bool read(signed int& value){
		return read((unsigned int &)(value));
	}
	bool read(signed long& value){
		return read((unsigned long &)(value));
	}
	bool read(signed long long& value){
		return read((unsigned long long &)(value));
	}

